package com.mygdx.game;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.mygdx.game.resource.Res;
import com.mygdx.game.screen.GameScreen;

public class MainGame extends Game {

    //世界的宽高
    private float worldWidth;
    private float worldHeight;

    //游戏场景
    private GameScreen gameScreen;
    //资源管理器
    private AssetManager assetManager;
    //纹理图集
    private TextureAtlas textureAtlas;
    //字体
    private BitmapFont bitmapFont;

    @Override
    public void create() {

        worldWidth = Res.FIX_WORLD_WIDTH;
        worldHeight = Gdx.graphics.getHeight() * worldWidth / Gdx.graphics.getWidth();

        //加载资源
        assetManager = new AssetManager();
        assetManager.load(Res.Audio.MOVE, Sound.class);
        assetManager.load(Res.Audio.MERGE, Sound.class);
        assetManager.load(Res.ATLAS_PATH, TextureAtlas.class);
        assetManager.load(Res.BITMAP_FONT_PATH, BitmapFont.class);
        assetManager.finishLoading();

        //获取资源
        textureAtlas = assetManager.get(Res.ATLAS_PATH, TextureAtlas.class);
        bitmapFont = assetManager.get(Res.BITMAP_FONT_PATH, BitmapFont.class);

        gameScreen = new GameScreen(this);
        //设置游戏场景
        setScreen(gameScreen);

        //捕捉系统返回键
        Gdx.input.setCatchKey(Input.Keys.BACK, true);

    }

    public GameScreen getGameScreen() {
        return gameScreen;
    }

    public float getWorldHeight() {
        return worldHeight;
    }

    public float getWorldWidth() {
        return worldWidth;
    }

    public TextureAtlas getTextureAtlas() {
        return textureAtlas;
    }

    public BitmapFont getBitmapFont() {
        return bitmapFont;
    }

    public AssetManager getAssetManager() {
        return assetManager;
    }

    @Override
    public void dispose() {
        super.dispose();
        //释放资源
        if (assetManager != null){
            assetManager.dispose();
        }
        if (textureAtlas != null){
            textureAtlas.dispose();
        }
        if (gameScreen != null){
            gameScreen.dispose();
        }
    }
}
